/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package htmlunit.code;

import java.awt.Image;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;

/**
 *
 * @author TiTa
 */
public class tai_hinh_dung_URL {
     static String  name;
    public tai_hinh_dung_URL(String name1){
        this.name=name1;
    }
    tai_hinh_dung_URL() {
       
    }
       //Lay gio he thong
Date gioD=new Date(System.currentTimeMillis()); 

//Khai bao dinh dang ngay thang
SimpleDateFormat đinhangngay= new SimpleDateFormat("dd MM yyyy"); 

//parse ngay thang sang dinh dang va chuyen thanh string.
String homnayS=đinhangngay.format(gioD.getTime());  
    public void tai_hinh(String url1) throws MalformedURLException, IOException{
         Image image;
//url1="https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-frc3/t1.417/1798728_10151940599516951_1383489559_n.jpg";
    URL url = new URL(url1);
    image = ImageIO.read(url);



    InputStream in = new BufferedInputStream(url.openStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    
    int n = 0;
    while (-1!=(n=in.read(buf)))
        {
            out.write(buf, 0, n);
        }
        out.close();
        in.close();
        byte[] response = out.toByteArray();
        FileOutputStream fos = new FileOutputStream("C://hinh_facebook//"+"hinh"+name+buf.toString()+".jpg");
        fos.write(response);
     //   System.out.print("tải hình xong");
        fos.close();
    
    }
  //   public static void main (String args[]) throws MalformedURLException, IOException  {
  // tai_hinh_dung_URL th=new tai_hinh_dung_URL();
  // th.tai_hinh("http://media6.tiin.vn/media01/4e66cf5ea8290/2013/04/01/9e8cce5f-06f1-4ef8-afe1-8364a9d900fe.jpg");

}